# Filters added to this controller apply to all controllers in the application.
# Likewise, all the methods added will be available for all controllers.

class ApplicationController < ActionController::Base
  helper :all # include all helpers, all the time
  protect_from_forgery # See ActionController::RequestForgeryProtection for details

  before_filter :authorize, :except=>[:signup,:start,:signin,:reset_password,:verify]
  
  EmailVerifyCodeLife=259200  #Email VerifiyCode has 72 hours of life
  MAX_IMAGE_SIZE=51200        #Maximize picture size is 51200
  
  protected
  def authorize
    unless User.find_by_id(session[:user_id])
      flash[:notice]=info(2,"Please sign in")
      redirect_to :controller=>'account',:action=>'signin'
    end
    @user = User.find(session[:user_id]) if session[:user_id]
  end

  def info(type,message)
    if type==0    #success
      "<img src=/images/succ.png width=24 height=24> "+message
    elsif type==1 #attention
      "<img src=/images/attn.png width=24 height=24> "+message
    elsif type==2 #failure
      "<img src=/images/fail.png width=24 height=24> "+message
    end
  end

  def show_errors(body)
      msg=""
      body.errors.full_messages.each do |t|
        msg+=t+"<br>"
      end
    msg
  end

  # Scrub sensitive parameters from your log
  # filter_parameter_logging :password
end
